package com.caishi.lkx.exam.service.impl;

import cn.hutool.core.util.StrUtil;
import com.caishi.lkx.exam.mapper.QuestionTypeMapper;
import com.caishi.lkx.exam.model.QuestionTypeModel;
import com.caishi.lkx.exam.service.IQuestionTypeService;
import com.zzw.common.entity.IBaseMapper;
import com.caishi.lkx.exam.ienum.type.QuestionType;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author by Janisa
 * @Description:
 * @date 2022/2/14 18:02
 */
@Service
@Slf4j
public class QuestionTypeServiceImpl implements IQuestionTypeService {

    @Resource
    private QuestionTypeMapper questionTypeMapper;

    @Override
    public IBaseMapper<QuestionTypeModel, String> getMapper() {
        return questionTypeMapper;
    }

    private static final ConcurrentHashMap<String, QuestionTypeModel> CACHE = new ConcurrentHashMap<>(64);

    @Override
    public String queryName(String s) {
        var res = CACHE.get(s);
        if (res != null) return res.getName();
        res = getById(s);
        if (res == null) return null;
        CACHE.put(s, res);
        return res.getName();
    }


    @Override
    public String queryName(String typeId, QuestionType questionType) {
        if (typeId == null) return questionType == null ? null : questionType.getDesc();
        var s = queryName(typeId);
        return StrUtil.isEmpty(s) ? questionType == null ? null : questionType.getDesc() : s;
    }
}
